package com.vietnam.power.util;

import java.util.Locale;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.web.authentication.session.SessionAuthenticationException;

public class MessageUtil {

	public static String getLoginErrorMessage(Exception exception) {
		String message = StringUtils.EMPTY;

		if (exception instanceof BadCredentialsException) {
			message = "admin.login.invalid";
		} else if (exception instanceof LockedException) {
			message = "admin.login.account.lock";
		} else if (exception instanceof AccountExpiredException) {
			message = "admin.login.account.expired";
		} else if (exception instanceof CredentialsExpiredException) {
			message = "admin.login.password.expired";
		} else if (exception instanceof SessionAuthenticationException) {
			message = "admin.login.exceed.browser";
		} else if (exception instanceof AuthenticationServiceException){
			message = "admin.login.invalid";
		} else {
			message = "admin.login.failed.other";
		}

		return message;
	}

	public static String getLocaleAsString() {
		Locale currentLocale = LocaleContextHolder.getLocale();
		return currentLocale.getLanguage() + "_" + currentLocale.getCountry();
	}

	public static String generateTransactionRefNum(String timezone) {
		java.util.Date now = DateTimeUtil.getCurrentDateByTimezone(timezone);
		@SuppressWarnings({ "deprecation" })
		String txnRef = now.getDate() + "" + (now.getMonth() + 1) + "" + (now.getYear() + 1900) + "-" + now.getHours() + "" + now.getMinutes() + ""
				+ now.getSeconds();
		return txnRef;
	}
}
